import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class FormPage extends StatelessWidget {
  final SystemUiOverlayStyle _style =
      SystemUiOverlayStyle(statusBarColor: Colors.transparent);

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(_style);
    // TODO: implement build
    return MaterialApp(
      home: FormWidget(),
      theme: ThemeData(primarySwatch: Colors.yellow),
    );
  }
}

class FormWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _FormPage();
  }
}

class _FormPage extends State<StatefulWidget> {

  TextEditingController usernameController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
  GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Form Page"),
      ),
      body: Form(
          key: _formKey,
          autovalidate: true,
          child: Column(
            children: <Widget>[
              TextFormField(
                controller: usernameController,
                validator: (value) {
                  return value.trim().length > 0 ? null : "请输入用户名";
                },
                decoration: InputDecoration(
                    labelText: "Username",
                    hintText: "请输入用户名",
                    icon: Icon(Icons.account_circle)),
              ),
              TextFormField(
                controller: passwordController,
                validator: (value) {
                  return value.trim().length > 0 ? null : "请输入密码";
                },
                decoration: InputDecoration(
                    labelText: "Password",
                    hintText: "请输入密码",
                    icon: Icon(Icons.content_paste)),
              ),
              RaisedButton(child: Text("提交"),onPressed: () {

                if((_formKey.currentState as FormState).validate()){
                    debugPrint("验证成功,提交数据到服务器！");
                    debugPrint("usernameController data: " + usernameController.text);
                    debugPrint("passwordController data: " + passwordController.text);
                }

              }),
            ],
          )),
    );
  }
}
